﻿using UnityEngine;
using System.Collections;


public class PlayerBehaviour : MonoBehaviour
{
    public GameObject gun;
    public GameObject player;
    public PlayerController controller;
    public PlayerBehaviourState state;
    public CameraManager cameraMgr;
    public ChangeStage changeStageMgr;
    public Weapon weapon;

    protected Vector2 gunDirection; //枪支方向
    protected int numberOfJumpLeft;   //剩余可跳跃次数

    //跳跃相关
    protected float firstJumpDuration;
    protected float secondJumpDuration;
    protected float jumpColdTime;
    private float jumpHoldTime;
    private float jumpColdTimeCount;
    private float jumpHoldTimeCount;

    //移动参数
    protected float horizontalAxis;
    protected float horizontalSlowScale;
    protected int horizontalDirection;
    protected bool isFaceingRight;
    protected Vector2 mousePoint;
    protected Vector3 lookAtDirection;

    void init()
    {
        horizontalAxis = 0;
        horizontalSlowScale = 1;
        horizontalDirection = 1;
        isFaceingRight = true;

        player = gameObject;
        gun = gameObject;

        controller = player.GetComponent<PlayerController>();
        state = new PlayerBehaviourState();
        cameraMgr = Camera.main.GetComponent<CameraManager>();
        changeStageMgr = GameObject.Find("Render Camera").GetComponent<ChangeStage>();

        numberOfJumpLeft = state.NumberOfJumps;

        jumpColdTime = 0.05f;
        jumpColdTimeCount = 0f;
        jumpHoldTimeCount = 0f;
        jumpHoldTime = 0.7f;
    }

    void Awake()
    {
        init();
    }


    private void Update()
    {
        updateAnimator();

        if (jumpColdTimeCount < jumpColdTime)
        {
            jumpColdTimeCount += Time.deltaTime;
        }

        if (controller.state.JustGotGrounded)
        {
            numberOfJumpLeft = state.NumberOfJumps;
            jumpHoldTimeCount = 0F;
            jumpColdTimeCount = jumpColdTime;
            state.Jumping = false;
            state.DoubleJumping = false;
        }

        if (!state.IsDead)
        {
            horizontalMove();
            //updateGunRotation();

        }
        else
        {
            respawn();
        }
    }

    public void updateAnimator()
    {

    }

    public void setHorizontalAAxis(float horizontalAxis)
    {
        this.horizontalAxis = horizontalAxis;
    }

    public void horizontalMove() {
        horizontalDirection = (horizontalAxis > 0) ? 1 : -1;

        if(state.CanMoveFreely)
        {
            if(state.Jumping)
            {
                controller.setHorizontalVelocity(horizontalAxis * horizontalSlowScale * PersistParameter.speedRunBase);
                return;
            }
            controller.setHorizontalVelocity(PersistParameter.jumpHorizontalSlowScale * horizontalAxis * horizontalSlowScale * PersistParameter.speedRunBase);
        }
        else
        {
            Debug.Log(controller.Speed);
        }
    }

    public void updateGunRotation()
    {
        mousePoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        float lookAtAngle = Vector2.Angle(Vector2.right, mousePoint - (Vector2)transform.position);
        if (lookAtAngle > 90)
        {
            if (isFaceingRight)
                Flip();
            isFaceingRight = false;
            if (mousePoint.y >= transform.position.y)
            {
                gun.transform.eulerAngles = new Vector3(gun.transform.eulerAngles.x, gun.transform.eulerAngles.y, lookAtAngle - 180);
            }
            else
            {
                gun.transform.eulerAngles = new Vector3(gun.transform.eulerAngles.x, gun.transform.eulerAngles.y, 180 - lookAtAngle);
            }
        }
        else
        {
            if (!isFaceingRight)
                Flip();
            isFaceingRight = true;
            if (mousePoint.y >= transform.position.y)
            {
                gun.transform.eulerAngles = new Vector3(gun.transform.eulerAngles.x, gun.transform.eulerAngles.y, lookAtAngle);
            }
            else
            {
                gun.transform.eulerAngles = new Vector3(gun.transform.eulerAngles.x, gun.transform.eulerAngles.y, -lookAtAngle);
            }
        }
    }

    public void Flip()
    {
        gun.transform.localScale = new Vector3(gun.transform.localScale.x * (-1),1,1);
    }

    public void startJump()
    {
        if (!state.CanMoveFreely||!state.CanJump||controller.state.IsCollidingAbove || state.DoubleJumping || jumpColdTimeCount < jumpColdTime) return;

        state.JumpButtonPressed = true;

        jumpColdTimeCount = 0f;

        if (numberOfJumpLeft == state.NumberOfJumps)
        {
            state.Jumping = true;
            state.DoubleJumping = false;
            //速度设置要求更改,根据跳跃高度来设置速度
            controller.setVerticalVelocity(30);
        }
        else
        {
            state.Jumping = true;
            state.DoubleJumping = true;
            controller.setVerticalVelocity(30);
        }

        state.JustStartJump = true;
        numberOfJumpLeft -= 1;
    }

    public void holdJump()
    {
        if (!state.CanJump || controller.state.IsCollidingAbove || state.DoubleJumping || jumpColdTimeCount < jumpColdTime) return;

        if (state.JumpButtonPressed && (state.Jumping || state.DoubleJumping))

        {
            jumpHoldTimeCount += Time.deltaTime;
            controller.addVerticalExternalForce(50*(1-(jumpHoldTimeCount / jumpHoldTime)));
        }
    }

    public void stopJump()
    {
        state.JumpButtonPressed = false;
        jumpHoldTimeCount = 0f;
    }

    public void climbLadder()
    {
        if (!state.CanClimbeLadder || !state.LadderBehind || state.LadderClimbing) return;


    }

    //切换z轴上不同的场景
    public void changeDepthStage()
    {
        if (!state.DoorBehind || !state.CanChangeStage) return;

    }



    public void setSlowScale()
    {

    }

    public void respawn()
    {

    }

    public void dash()
    {
        StartCoroutine(invincibleChange());
    }

    public IEnumerator invincibleChange()
    {
        Vector3 dashSpeed = (new Vector3(10 * horizontalDirection, 0, 0)) / (4 * Time.fixedDeltaTime);
        state.CanMoveFreely = false;
        state.CanBeHurt = false;
        controller.state.StayInAir = true;
        controller.setVelocity(Vector2.zero);
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        Debug.Log(dashSpeed);
        controller.setVelocity(dashSpeed);
        state.IsDashing = true;
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        state.IsDashing = false;
        controller.setVelocity(Vector2.zero);
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        yield return new WaitForFixedUpdate();
        state.CanBeHurt = true;
        controller.state.StayInAir = false;
        state.CanMoveFreely = true;
    }

    public void fire()
    {

    }

    public void reload()
    {

    }
}
